﻿using AutoMapper;
using Blog.Core.Domain.Model.User;
using Blog.Core.DTO.ViewModel.User;
using Blog.Core.IRepositories;
using Blog.Core.IRepositories.IUser;
using Blog.Core.IServices.IUser;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Blog.Core.Services.User
{
    /// <summary>
    /// 权限信息服务
    /// </summary>
    public class PermissionsService : BaseService<Permissions>, IPermissionsService
    {
        private readonly IPermissionsRepository permissionsRepository;
        private readonly IUnOfWork unOfWork;
        private readonly IMapper mapper;

        public PermissionsService(IPermissionsRepository permissionsRepository, IUnOfWork unOfWork, IMapper mapper) : base(permissionsRepository, unOfWork)
        {
            this.mapper = mapper;
            this.permissionsRepository = permissionsRepository;
            this.unOfWork = unOfWork;
        }
        /// <summary>
        /// 树形方式显示，用于显示菜单，进行权限分配
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<ResponsePermissionTreeDTO>> GetPermission(int id)
        {
            List<ResponsePermissionTreeDTO> result = new List<ResponsePermissionTreeDTO>();
            var list = await permissionsRepository.GetAll();
            var entity = list.ToList().Where(x => x.Permissions_Pid == id);//&& x.IsMenu == true
            foreach (var item in entity)
            {
                result.Add(new ResponsePermissionTreeDTO
                {
                    Id = item.Id,
                    PermissionsName = item.PermissionsName,
                    children = await GetPermission(item.Id)
                });
            }
            return result.Count()>0?result:null;
        }
        /// <summary>
        /// 树形方式显示用于列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<List<ResponsePermissionListDTO>> GetPermissionList(int id)
        {
            List<ResponsePermissionListDTO> result = new List<ResponsePermissionListDTO>();
            var list = await permissionsRepository.GetAll();
            var entity = list.ToList().Where(x => x.Permissions_Pid == id);
            foreach (var item in entity)
            {
                result.Add(new ResponsePermissionListDTO
                {
                    Id = item.Id,
                    ApiId = item.ApiId,
                    Enabled = item.Enabled,
                    Func = item.Func,
                    Icon = item.Icon,
                    IsButton = item.IsButton,
                    IsHide = item.IsHide,
                    OrderSort = item.OrderSort,
                    RouteId = item.RouteId,
                    Permissions_Pid = item.Permissions_Pid,
                    PermissionsDescription = item.PermissionsDescription,
                    PermissionsName = item.PermissionsName,
                    children = await GetPermissionList(item.Id)
                });
            }
            return result;
        }
        ///// <summary>
        ///// 权限的添加
        ///// </summary>
        ///// <returns></returns>
        //public int PostPermissionInfo()
        //{

        //}

    }
}
